<?php

namespace app\admin\model\user;

use think\Model;
use traits\model\SoftDelete;

class Contract extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'user_contract';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'ya_type_text',
        'start_time_text',
        'end_time_text',
        'pay_time_text'
    ];
    

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '0' => __('Status 0')];
    }
    public function getYaTypeList()
    {
        return ['1' => __('押一付一'), '2' => __('押一付三')];
    }



    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getYaTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['ya_type']) ? $data['ya_type'] : '');
        $list = $this->getYaTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function homeowner()
    {
        return $this->belongsTo('app\admin\model\User', 'homeowner_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function building()
    {
        return $this->belongsTo('app\admin\model\Building', 'build_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function room()
    {
        return $this->belongsTo('app\admin\model\building\Room', 'room_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function house()
    {
        return $this->belongsTo('app\admin\model\House', 'house_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
